<?php
namespace App\HttpController;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\ORM\AbstractModel;

class Base extends Controller
{
    protected function argError( $msg = '参数错误！',int $code = 404)
    {
        $this->writeJson($code ? $code : 404,null,$msg);
    }


    protected function success($data = null,?string $msg = null )
    {
        if( \is_null( $data ) )
        {
            $msg = '操作成功！';
            $data = null;

        }
        else if( \is_string($data) )
        {
            $msg = $data;
            $data = null;

        }
        else if( \is_array($data))
        {
            $msg = $msg ?: '操作成功！';
        }
        else if( \is_object( $data ) )
        {
            if( $data instanceof AbstractModel){
                $data = $data->toArray();
            } else {
                $data = \get_object_vars($data);
            }

            $msg = '操作成功！';
        }
        $this->writeJson(200,$data,$msg);
    }
    public function index()
    {

       // TODO: Change the autogenerated stub
    }

    protected function onRequest(?string $action): ?bool
    {
       return true; // TODO: Change the autogenerated stub
    }

}